package me.xplabs.fight.layer 
{
	import flash.display.BitmapData;
	import flash.filters.BitmapFilter;
	import flash.filters.GlowFilter;
	import flash.utils.Dictionary;
	import me.xplabs.AppFacade;
	import me.xplabs.Globals;
	import me.xplabs.build.BuildDisplay;
	import me.xplabs.engine.display.BaseDisplay;
	import me.xplabs.engine.display.BaseDisplayContainer;
	import me.xplabs.event.MMouseEvent;
	import me.xplabs.model.combatunit.MBuildInfo;
	import me.xplabs.msg.NotificationName;
	import me.xplabs.utils.SimpleObjPool;
	import org.puremvc.as3.patterns.facade.Facade;
	
	/**
	 * 建筑层
	 * ...
	 * @author xiaohan
	 */
	public class BuildLayer extends BaseDisplayContainer 
	{
		private var _buildPool:SimpleObjPool;
		private var _builds:Vector.<BuildDisplay>;
		private var _glowFilter:BitmapFilter;
		public function BuildLayer() 
		{
			super();
			_buildPool = new SimpleObjPool(BuildDisplay, 1, 1);
			_builds = new Vector.<BuildDisplay>();
			_glowFilter = new GlowFilter(0xECF107, 0.5, 4, 4, 6, 1);
		}
		
		/*public function initBuildList(builds:Vector.<MBuildInfo>):void
		{
			var len:int = builds.length;
			for (var i:int = 0; i < len; i++) 
			{
				var tempBuild:BuildDisplay = _buildPool.getObj();
				
				tempBuild.addEventListener(MMouseEvent.ROLL_OVER, buildRollOverHandler);
				tempBuild.addEventListener(MMouseEvent.ROLL_OUT, buildRollOutHandler);
				tempBuild.buildInfo = builds[i];
				tempBuild.setAnimate(100, true, builds[i].frameInt);
				tempBuild.resLib[1] = [];
				tempBuild.resLib[1].push(Globals.lib.getResObj(Globals.domain.build +"r." + builds[i].resId));
				
			
				addChild(tempBuild);
				_builds.push(tempBuild);
			}
		}*/
		
		private function buildRollOutHandler(e:MMouseEvent):void 
		{
			var tempBuild:BuildDisplay = e.currentTarget as BuildDisplay;
			tempBuild.applyFilter();
			sendMsg(NotificationName.ROLL_OUT_BUILD, tempBuild.buildInfo);
		}
		
		private function buildRollOverHandler(e:MMouseEvent):void 
		{
			var tempBuild:BuildDisplay = e.currentTarget as BuildDisplay;
			tempBuild.applyFilter(_glowFilter);
			sendMsg(NotificationName.ROLL_OVER_BUILD, tempBuild.buildInfo);
		}
		public function upgradeBuild(p_buildInfo:MBuildInfo):void
		{
			var len:int = _builds.length;
			for (var i:int = 0; i < len; i++) 
			{
				if (_builds[i].buildInfo.soleId == p_buildInfo.soleId)
				{
					var tempBuild:BuildDisplay = _builds[i];
					tempBuild.buildInfo = p_buildInfo;
					tempBuild.setAnimate(100, true, 1);
					if (!tempBuild.resLib[1]) tempBuild.resLib[1] = [];
					else tempBuild.resLib[1].length = 0;
					//tempBuild.resLib[1].push(Globals.lib.getResObj(Globals.domain.build +"i" + p_buildInfo.resId));
					var resBitmapData:* = Globals.lib.getResObj(Globals.domain.build +"r" + p_buildInfo.resId + ".i1");
					if (resBitmapData)
					{
						tempBuild.resLib[1].push(resBitmapData);
						var tempLen:int = resBitmapData.length;
						for (var j:int = 1; j < tempLen; j++) 
						{
							tempBuild.resLib[1].push(Globals.lib.getResObj(Globals.domain.build +"r" + p_buildInfo.resId + ".i" + (j + 1)));
						}
					}
					break;
				}
			}
		}
		public function destoryBuilding(p_buildInfo:MBuildInfo):void
		{
			upgradeBuild(p_buildInfo);
		}
		public function delBuilding(p_soleId:int):void
		{
			var len:int = _builds.length;
			for (var i:int = 0; i < len; i++) 
			{
				if (_builds[i].buildInfo.soleId == p_soleId)
				{
					_builds[i].dispose();
					_builds[i].removeEventListener(MMouseEvent.CLICK, buildClickHandler);
					_builds[i].removeEventListener(MMouseEvent.ROLL_OVER, buildRollOverHandler);
					_builds[i].removeEventListener(MMouseEvent.ROLL_OUT, buildRollOutHandler);
					if (_builds[i].parent) _builds[i].parent.removeChild(_builds[i]);
					_buildPool.dispose(_builds.splice(i, 1)[0]);
					break;
				}
			}
			
		}
		
		private function buildClickHandler(e:MMouseEvent):void 
		{
			print("点击建筑" + e.type + (e.currentTarget as BuildDisplay).buildInfo.soleId);
			sendMsg(NotificationName.CLICK_BUILDING, (e.currentTarget as BuildDisplay).buildInfo);
		}

		public function addBuilding(p_buildInfo:MBuildInfo):void 
		{
			var tempBuild:BuildDisplay = _buildPool.getObj();
			//tempBuild.addEventListener(MMouseEvent.CLICK, buildClickHandler);
			tempBuild.addEventListener(MMouseEvent.CLICK, buildClickHandler);
			tempBuild.addEventListener(MMouseEvent.ROLL_OVER, buildRollOverHandler);
			tempBuild.addEventListener(MMouseEvent.ROLL_OUT, buildRollOutHandler);
			tempBuild.buildInfo = p_buildInfo;
			tempBuild.buttonMode = true;
			//tempBuild.setAnimate(100, true, p_buildInfo.frameInt);
			tempBuild.setAnimate(100, true, 1);
			tempBuild.resLib[1] = [];
			//tempBuild.resLib[1].push(Globals.lib.getResObj(Globals.domain.build +"i" + p_buildInfo.resId));
			
			var resBitmapData:* = Globals.lib.getResObj(Globals.domain.build +"r" + p_buildInfo.resId + ".i1");
			if (resBitmapData)
			{
				tempBuild.resLib[1].push(resBitmapData);
				var len:int = resBitmapData.length;
				for (var i:int = 1; i < len; i++) 
				{
					tempBuild.resLib[1].push(Globals.lib.getResObj(Globals.domain.build +"r" + p_buildInfo.resId + ".i" + (i + 1)));
				}
			}
			/*for (var j:int = 1; j < 5; j++) 
			{
				tempBuild.resLib[1].push(Globals.lib.getResObj(Globals.domain.magic + "r100010_4" + ".i" + j));
			}*/
			addChild(tempBuild);
			_builds.push(tempBuild);
			
		}
	}
}